// components/AutoLogin.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        hasAuth: true
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getUser(res) {
            if (res.detail.userInfo) {
                this.nickName = res.detail.userInfo.nickName,
                    this.photo = res.detail.userInfo.avatarUrl,
                    this.userInfo = res.detail.userInfo
                wx.setStorage({
                    key: 'user',
                    data: res.detail.userInfo,
                    success: res => {
                        this.reload()
                    }
                })
                this.setData({
                    hasAuth: true,
                })
            }

        },
        reload() {
            var pages = getCurrentPages()
            var currentPage = pages[pages.length - 1]
            currentPage.onLoad(currentPage.options) || currentPage.onShow()
            setTimeout(() => {
                this.saveInfo()
            }, 1000)
        },
        saveInfo() {
            getApp().http.post('city/tiny-station/save-info', {
                data: {
                    avatarUrl: this.userInfo.avatarUrl,
                    nickName: this.userInfo.nickName
                }
            })
            console.log('abcdefg')
        }
    },

    attached() {
        wx.getSetting({
            success: res => {
                console.log(res, '******')
                if (res.authSetting['scope.userInfo']) {
                    //已经授权
                    if (wx.getStorageSync('user')) {
                        this.setData({
                            hasAuth: true,
                        })
                    } else {
                        wx.getUserInfo({
                            success: res => {
                                wx.setStorage({
                                    key: 'user',
                                    data: res.userInfo,
                                    success: q => {
                                        this.nickName = res.userInfo.nickName,
                                            this.photo = res.userInfo.avatarUrl,
                                            this.userInfo = res.userInfo
                                        this.reload()
                                    }
                                })
                                this.setData({
                                    hasAuth: true,
                                })
                            }
                        })
                    }
                } else {
                    this.setData({
                        hasAuth: false,
                    })
                }
            }
        })
    },


})